修改文章是网站必不可少的内容,在我的文章里,文章列表后面操作栏有【修改】链接,点击调转到[UserEdit]Action,今天来实现这个Action。
还是打开【ArticleController】,添加[UserEdit(int id)]Action这里的id指文章Id。
////// 修改文章 /// /// 文章id [UserAuthorize] public ActionResult UserEdit(int id) { return View(articleRsy.Find(id)); }
@model Ninesky.Models.Article@{ ViewBag.Title = "修改文章"; Layout = "~/Views/Shared/_User.cshtml";}@using (Html.BeginForm()) { @Html.ValidationSummary(true)}
- 修改文章
@Html.HiddenFor(model => model.CommonModelId) @Html.HiddenFor(model => model.CommonModel.CommonModelId) @Html.LabelFor(model => model.CommonModel.CategoryId) (必填) @Html.DisplayDescriptionFor(model => model.CommonModel.CategoryId)@Html.HiddenFor(model => model.CommonModel.CategoryId) @Html.ValidationMessageFor(model => model.CommonModel.CategoryId)
@Html.HiddenFor(model => model.ArticleId) @Html.LabelFor(model => model.CommonModel.Title) (必填) @Html.DisplayDescriptionFor(model => model.CommonModel.Title)@Html.EditorFor(model => model.CommonModel.Title) @Html.ValidationMessageFor(model => model.CommonModel.Title) @Html.LabelFor(model => model.Source)@Html.DisplayDescriptionFor(model => model.Source)@Html.EditorFor(model => model.Source) @Html.ValidationMessageFor(model => model.Source) @Html.LabelFor(model => model.Intro)@Html.DisplayDescriptionFor(model => model.Intro)@Html.TextAreaFor(model => model.Intro, new { @class = "Intro" }) @Html.ValidationMessageFor(model => model.Intro) @Html.LabelFor(model => model.CommonModel.PicUrl)@Html.DisplayDescriptionFor(model => model.CommonModel.PicUrl)@Html.EditorFor(model => model.CommonModel.PicUrl) @Html.ValidationMessageFor(model => model.CommonModel.PicUrl) @Html.LabelFor(model => model.CommonModel.Status)@Html.DisplayDescriptionFor(model => model.CommonModel.Status)@Html.DropDownListFor(model => model.CommonModel.Status,Ninesky.Models.CommonModel.ContentStatus) @Html.ValidationMessageFor(model => model.CommonModel.Status) @Html.LabelFor(model => model.CommonModel.ReleaseDate) (必填) @Html.DisplayDescriptionFor(model => model.CommonModel.ReleaseDate)@Html.TextBoxFor(model => model.CommonModel.ReleaseDate, new { @class = "datepicker", type = "datetime" }) @Html.ValidationMessageFor(model => model.CommonModel.ReleaseDate) @Html.LabelFor(model => model.Content) (必填) @Html.DisplayDescriptionFor(model => model.Content)@Html.TextAreaFor(model => model.Content, new { @class = "content" }) @Html.ValidationMessageFor(model => model.Content) @Html.Partial("PartialUserNavMenus")@section Scripts { @Styles.Render("~/ZtreeCss") @Scripts.Render("~/bundles/jqueryval") @Scripts.Render("~/Ztree") @Scripts.Render("~/bundles/kindeditor")}
修改文章处理action UserEdit(Article article)
[HttpPost] [UserAuthorize] [ValidateInput(false)] public ActionResult UserEdit(Article article) { //验证栏目 CategoryRepository _categoryRsy = new CategoryRepository(); var _category = _categoryRsy.Find(article.CommonModel.CategoryId); if (_category == null) ModelState.AddModelError("CommonModel.CategoryId", "栏目不存在"); if (_category.Model != "Article") ModelState.AddModelError("CommonModel.CategoryId", "该栏目不能添加文章!"); article.CommonModel.Inputer = UserController.UserName; ModelState.Remove("CommonModel.Inputer"); article.CommonModel.Model = "Article"; ModelState.Remove("CommonModel.Model"); if (ModelState.IsValid) { var _article = articleRsy.Find(article.ArticleId); if (_article == null)//文章不存在 { Error _e = new Error { Title = "文章不存在", Details = "查询不到ArticleId为【" + article.ArticleId.ToString() + "】的文章", Cause = "文章已被删除或向服务器提交文章时数据丢失", Solution = Server.UrlEncode("
这个action也是先验证栏目是否存在,且能添加文章。呵呵,跟添加文章的基本类似。
按F5 浏览器修改篇文章看,能够修改,ok!
===========================
代码见:
===========================
1月26日修改:
今天仔细看的时候发现一个问题。在“我的文章”修改链接代码为:@Html.ActionLink("修改","UserEdit",new {id = item.CommonModelId}),这里的id是公共模型的id,再看看ActionResult UserEdit(int id)里面的代码,文章是articleRsy.Find(id))查询返回的。转到定义看看一下代码。
////// 查找文章 /// /// 文章id ///public override Article Find(int Id) { return dbContext.Articles.AsNoTracking().Include("CommonModel").SingleOrDefault(a => ); }
这里的id是文章id。所以是用驴唇对了马嘴.... ....
怎么改正呢?既然ActionResult UserEdit(int id)的id指的是公共模型的id,那就写个根据公共模型id查找文章的函数FindByCommonModelId(int commonModelId)就行了。
在ArticleRepository中添加FindByCommonModelId(int commonModelId)函数。
////// 根据公共模型id查找文章 /// /// 公共模型Id ///文章 public Article FindByCommonModelId(int commonModelId) { return dbContext.Articles.AsNoTracking().Include("CommonModel").SingleOrDefault(a => a.CommonModelId == commonModelId); }
将UserEdit(int id)中的return View(articleRsy.Find(id))改为return View(articleRsy.FindByCommonModelId(id))
////// 修改文章 /// /// 公共模型id [UserAuthorize] public ActionResult UserEdit(int id) { return View(articleRsy.FindByCommonModelId(id)); }